#引脚连接
#SCL (SPI Clock): 连接到 ESP32 的 GPIO18
#SDA (SPI Data): 连接到 ESP32 的 GPIO23
#RES (Reset): 连接到 ESP32 的 GPIO4
#DC (Data/Command): 连接到 ESP32 的 GPIO2
#CS (Chip Select): 连接到 ESP32 的 GPIO5
#BUSY: 连接到 ESP32 的 GPIO15

from machine import Pin, SPI
from math import sqrt
from fonts import asc2_0806
from ssd1680z import SSD1680, Color
from ssd1680z import Rotate


def init(rotate=Rotate.ROTATE_0):
    busy = Pin(15, Pin.IN)
    res = Pin(4, Pin.OUT)
    cs = Pin(5, Pin.OUT)
    dc = Pin(2, Pin.OUT)

    spi_ssd1680 = SPI(
        1,
        baudrate=400000,
        polarity=0,
        phase=0,
        sck=Pin(18),
        mosi=Pin(23),
        miso=Pin(19)
    )

    ssd1680 = SSD1680(
        spi_ssd1680,
        dc,
        busy,
        cs,
        res,
        rotate
    )

    ssd1680.init()
    ssd1680.clear(Color.WHITE)
    return ssd1680

if __name__ == "__main__":  # test
    ssd1680 = init()
    ssd1680.show_string("11:32", 0, 10, multiplier=2)
    ssd1680.update()

    #ssd1680.test_partial_update(0XFF)

    #ssd1680.clear(Color.WHITE)
    #ssd1680.show_string("17:33", 0, 50, multiplier=2)
    #ssd1680.draw_line(40, 20, 110, 20)
    #ssd1680.update()
    #sleep(30)
    
    # ssd1680.draw_line(250, 100, 110, 20)
    # ssd1680.draw_rectangle(18, 18, 28, 28)
    # ssd1680.draw_rectangle(18, 38, 94, 48)
    # ssd1680.draw_circle(60, 20, 10)
    # ssd1680.draw_circle(90, 20, 10)
    # ssd1680.show_char('h', 20, 20)
    # ssd1680.show_string("hello world!", 20, 40)
    # ssd1680.show_string("hello world!", 20, 60, multiplier=2)
    # ssd1680.show_string("hello world!", 20, 90, multiplier=3)
    # ssd1680.show_string("Godfly 1.9.2023", 150, 45)
    # ssd1680.show_bitmap(
    #         [
    #             [0,0,1,0,0],
    #             [0,0,1,0,0],
    #             [1,1,0,1,1],
    #             [0,0,1,0,0],
    #             [0,0,1,0,0]
    #         ],
    #         150,
    #         10,
    #         multiplier=3
    #     )
    # ssd1680.show_bitmap(
    #         [
    #             [0,0,1,0,0,0,1,0,0],
    #             [0,1,0,1,0,1,0,1,0],
    #             [1,0,0,0,1,0,0,0,1],
    #             [0,1,0,0,0,0,0,1,0],
    #             [0,0,1,0,0,0,1,0,0],
    #             [0,0,0,1,0,1,0,0,0],
    #             [0,0,0,0,1,0,0,0,0]
    #         ],
    #         150,
    #         80,
    #         multiplier=4
    #     )
    